package com.jinyu.jyly.ui.mine.suggest;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.jinyu.jyly.R;
import com.jinyu.jyly.adapter.SuggestListAdapter;
import com.jinyu.jyly.base.BaseActivity;
import com.jinyu.jyly.bean.AllInfoBean;
import com.jinyu.jyly.bean.SuggestListInfo;
import com.jinyu.jyly.utils.AES;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 意见反馈
 */
public class SuggestActivity extends BaseActivity<SuggestPresenter> implements SuggestContract.View {

    @BindView(R.id.fake_status_bar)
    View fakeStatusBar;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rl_title_view)
    RelativeLayout rlTitleView;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.btn_sub)
    Button btnSub;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private SuggestPresenter presenter;

    @Override
    public int getLayout() {
        return R.layout.activity_suggest;
    }

    @Override
    protected SuggestPresenter getPresenter() {
        if (presenter == null) {
            presenter = new SuggestPresenter(this, this);
        }
        return presenter;
    }

    @Override
    protected void initEventAndData() {
        QMUIStatusBarHelper.translucent(this);
        QMUIStatusBarHelper.setStatusBarDarkMode(this);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        presenter.getSuggestList();

    }

    @OnClick({R.id.iv_back, R.id.btn_sub})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_sub:
                String content = etContent.getText().toString();
                if (TextUtils.isEmpty(content)) {
                    showToast("反馈意见不能为空");
                    return;
                }

                try {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("title", "意见");
                    jsonObject.put("content", content);
                    String pamam = AES.encryptToHexString(jsonObject.toString());
                    presenter.setSuggest(pamam);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    @Override
    public void onFailer(Throwable throwable) {

    }

    @Override
    public void onSetSuggest(AllInfoBean infoBean) {
        showToast("提交成功");
        etContent.setText("");
        presenter.getSuggestList();
    }

    /**
     * 意见反馈列表
     * @param info
     */
    @Override
    public void onSuggestList(AllInfoBean info) {
        switch (info.getStatus()){
            case 200:
                String data = info.getData();
                String result = null;
                try {
                    result = AES.decrypt(data);
                    Gson gson = new Gson();
                    Type listType = new TypeToken<ArrayList<SuggestListInfo>>() {}.getType();
                    List<SuggestListInfo> mList = gson.fromJson(result, listType);
                    if(mList != null && mList.size() != 0){
                        SuggestListAdapter suggestListAdapter = new SuggestListAdapter(R.layout.item_suggest_list, mList);
                        recyclerView.setAdapter(suggestListAdapter);
                    }else{

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                showToast(info.getMsg());
                break;
            default:break;
        }
    }
}
